/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						 teleport_ability.cpp

	$Header: /heroes4/teleport_ability.cpp $

	$NoKeywords: $

 ************************************************************************/
// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "teleport_ability.h"

#include "battlefield.h"
#include "combat_creature.h"
#include "game_time.h"
#include "options.h"
// -----------------------------------------------------------------
// display a teleport movement
// -----------------------------------------------------------------
t_teleport_ability::t_teleport_ability( t_combat_creature&			creature, 
									    t_map_point_3d const&		destination,
										t_combat_creature_handler   end_handler )
				  : t_counted_animation( creature.get_battlefield() ), m_creature( creature ),
					t_counted_idle_processor( 20 )
{
	t_direction					direction;
	t_battlefield&				battlefield = m_battlefield;

	m_destination = destination;
	m_end_handler = end_handler;
	direction = get_direction( creature.get_position(), destination );
	m_battlefield.set_current_direction_and_action( creature, direction, 
		                                            k_combat_actor_action_prewalk );
	m_creature.play_sound( k_combat_actor_action_prewalk );
	m_creature.clear_influence();
	set_delay( 100000 / (creature.get_frames_per_second() * get_combat_animation_speed()));
}

// -----------------------------------------------------------------
// display a teleport movement
// -----------------------------------------------------------------
void t_teleport_ability::on_idle()
{
	t_combat_actor_action_id action = m_creature.get_current_action();
	t_battlefield&			 battlefield = m_battlefield;

	if (action == k_combat_actor_action_wait 
		|| action == k_combat_actor_action_base_frame)
	{
		m_creature.set_influence();
		suspend_idle_processing();
		animation_ended();
		battlefield.check_gate_close();
		m_end_handler( m_creature );
		return;
	}

	int frame	    = m_creature.get_current_frame_num();
	int frame_count = m_creature.get_frame_count();

	frame++;
	if (frame < frame_count)
	{
		battlefield.set_current_frame_num( m_creature, frame );
		return;
	}
	if (action == k_combat_actor_action_prewalk)
	{
		battlefield.move_object( m_creature, m_destination );
		battlefield.set_current_action( m_creature, k_combat_actor_action_postwalk );
		set_delay( 100000 / (m_creature.get_frames_per_second() * get_combat_animation_speed()));
		m_creature.play_sound( k_combat_actor_action_postwalk );
		if (battlefield.get_tower( m_destination >> k_battlefield_subcell_shift,
			                       m_creature.get_footprint()) != 0)
			m_creature.place_in_tower();
		return;
	}
	if (m_creature.has_action( k_combat_actor_action_base_frame ))
		battlefield.set_current_action( m_creature, k_combat_actor_action_base_frame );
	else
		battlefield.set_current_action( m_creature, k_combat_actor_action_wait );
}
